﻿using Microsoft.WindowsAPICodePack.Dialogs;
using Microsoft.WindowsAPICodePack.Shell;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFKnownFolders
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadCombox();
        }

        private void openLB_Click(object sender, RoutedEventArgs e)
        {
            string selection = libraries.SelectedItem as string;
            ShellContainer selectedFolder = null;
            switch (selection)
            {
                case "Documents":
                    selectedFolder = KnownFolders.DocumentsLibrary as ShellContainer;
                    break;
                case "Music":
                    selectedFolder = KnownFolders.MusicLibrary as ShellContainer;
                    break;
                case "Pictures":
                    selectedFolder = KnownFolders.PicturesLibrary as ShellContainer;
                    break;
                case "Videos":
                    selectedFolder = KnownFolders.VideosLibrary as ShellContainer;
                    break;
            }

            CommonOpenFileDialog cfd = new CommonOpenFileDialog();
            cfd.EnsureReadOnly = true;
            cfd.InitialDirectoryShellContainer = selectedFolder;
            cfd.ShowDialog();
        }

        private void openKF_Click(object sender, RoutedEventArgs e)
        {
            CommonOpenFileDialog cfd = new CommonOpenFileDialog();
            string kfString = knowFolders.SelectedItem as string;
            IKnownFolder kf = KnownFolderHelper.FromCanonicalName(kfString);
            cfd.InitialDirectoryShellContainer = kf as ShellContainer;
            cfd.ShowDialog();
        }

        private void LoadCombox()
        {
            knowFolders.Items.Clear();
            foreach (IKnownFolder kf in KnownFolders.All)
            {
                if (kf != null && kf.CanonicalName != null)
                {
                    knowFolders.Items.Add(kf.CanonicalName);
                }
            }

            if (knowFolders.Items.Count > 0)
            {
                SortDescription kfSort = new SortDescription();
                knowFolders.Items.SortDescriptions.Add(kfSort);
                knowFolders.SelectedIndex = 0;
            }

            libraries.Items.Clear();
            libraries.Items.Add("Documents");
            libraries.Items.Add("Music");
            libraries.Items.Add("Pictures");
            libraries.Items.Add("Videos");
            libraries.SelectedIndex = 0;
        }
    }
}
